﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Film.Common;

namespace Film.Repository
{
    public class tblUserAccountRepository : Repository<tblUserAccount>
    {
        /// <summary>
        /// Lấy những thằng admin
        /// </summary>
        /// <param name="keyWord"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<tblUserAccount> Filter(string keyWord, int start, int limit, out int count, bool isAdmin = true)
        {
            var result = (from ds in dataContext.tblUserAccounts
                          where (keyWord.Trim() == "" || (ds.Email.Contains(keyWord) || ds.FullName.Contains(keyWord) || ds.UserName.Contains(keyWord))) && ds.IsAdmin == isAdmin
                          select ds);
            count = result.Count();
            return result.Skip(start).Take(limit).ToList();

        }

        public tblUserAccount GetLogin(string userName, string passWord)
        {
            //passWord = EncryptDecryptUtil.GetMd5Sum(userName + passWord);
            var result = dataContext.tblUserAccounts.FirstOrDefault(x => x.UserName == userName && x.PassWord == passWord); // && x.PassWord == passWord);
            return result;
        }

        public bool CheckUserName(string userName, int ID)
        {
            return dataContext.tblUserAccounts.Where(x => x.UserName == userName && x.UserID != ID).Count() > 0;
        }

        public bool CheckEmail(string email, int ID)
        {
            return dataContext.tblUserAccounts.Where(x => x.Email == email && x.UserID != ID).Count() > 0;
        }

        public tblUserAccount GetByUserId(int ID)
        {
            return dataContext.tblUserAccounts.FirstOrDefault(x => x.UserID == ID);
        }
        public tblUserAccount GetByUserName(string userName)
        {
            return dataContext.tblUserAccounts.FirstOrDefault(x => x.UserName == userName);
        }

        public int GetNextUserID()
        {
            int userIDMax = dataContext.tblUserAccounts.Max(u => u.UserID);
            return userIDMax = 1;
        }
    }
}
